package com.typy.www.tpay.ui.itemview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.vm.ActivicationVm;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.ui.acitivity.WebActivity;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.PopupUtil;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/28.
 */

public class RecordDigiItemView {
    View mView;
    ViewHolder holder;
    private Context mContext;
    private PopupWindow popupWindow;

    public RecordDigiItemView(Context context) {
        mContext = context;

        mView = LayoutInflater.from(context).inflate(R.layout.item_digi_recode, null);
        holder = new ViewHolder(mView);
    }

    public View getView() {
        return mView;
    }

    public void bind(final ActivicationVm vm, final int position, final int classType) {
        holder.imgDiv.setVisibility(View.VISIBLE);
        if (vm != null)
            if (classType == ClassHoldUtil.DigiDetail) {
                holder.llHolder.setVisibility(View.VISIBLE);
                holder.llHolder2.setVisibility(View.GONE);
                UpdataUiUtil.getDefaul().setText(vm.getFdTypeName(), holder.tvTypeName)
                        .setText(vm.getFdRemark(), holder.tvContent)
                        .setText("数量:" + vm.getFdIconCount() + "个", holder.tvShuliang)
                        .setText(vm.getFdCreateTime(), holder.tvTime);
            } else if (classType == ClassHoldUtil.DIGIDINGDANG || classType == ClassHoldUtil.DIGIDINGDANGNOW || classType == ClassHoldUtil.DIGIDINGDANGEND) {
                holder.llHolder.setVisibility(View.GONE);
                holder.llHolder2.setVisibility(View.VISIBLE);
                String zonge = Double.valueOf(vm.getFdPrice()) * Double.valueOf(vm.getFdRemainQuantity()) + "";
                UpdataUiUtil.getDefaul().setText("总额：" + vm.getFdTotalAmount() + "[" + vm.getFdTypeName() + "]", holder.tvTypeName2)
                        .setText("¥" + vm.getFdPrice(), holder.tvContent2)
                        .setText("剩余:" + vm.getFdRemainQuantity() + "个", holder.tvTime2)
                        .setText(vm.getFdCreateTime(), holder.tvShuliang2);
                if (classType == ClassHoldUtil.DIGIDINGDANGEND)
                    holder.tvTime2.setVisibility(View.GONE);
                else
                    holder.tvTime2.setVisibility(View.VISIBLE);
                holder.tvTime2.setTextColor(mContext.getResources().getColor(R.color.text_moneyred));
                holder.tvShuliang2.setTextColor(mContext.getResources().getColor(R.color.text_lightcolor));
                holder.tvContent2.setTextColor(mContext.getResources().getColor(R.color.text_darkcolor));
            }
        holder.llTouxian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (classType == ClassHoldUtil.DigiDetail || classType == ClassHoldUtil.DIGIDINGDANGEND || classType == ClassHoldUtil.DIGIDINGDANG) {
                    WebActivity.show(mContext, "详情记录", vm.getFdUrl());
                } else if (classType == ClassHoldUtil.DIGIDINGDANGNOW) {
                    View contentView = LayoutInflater.from(mContext).inflate(
                            R.layout.photo_action_selector, null);
                    popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT, true);
                    PopupUtil.showPopup(mContext, popupWindow, contentView, "详情", "撤销订单", "取消", new PopupUtil.PopupListener() {
                        @Override
                        public void clickOne() {
                            popupWindow.dismiss();
                            WebActivity.show(mContext, "详情记录", vm.getFdUrl());
                        }
                        @Override
                        public void clickTow() {
                            popupWindow.dismiss();
                            Logutil.print("取消订单");
                            EventBus.getDefault().post(new MessageEvent(MessageAction.CANCELORDER, vm.getFdId()));
                        }
                        @Override
                        public void clickCancle() {
                            popupWindow.dismiss();
                        }
                    });
                }
            }
        });
    }


    static class ViewHolder {
        @BindView(R.id.tv_type_name)
        TextView tvTypeName;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.tv_shuliang)
        TextView tvShuliang;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.ll_holder)
        PercentLinearLayout llHolder;
        @BindView(R.id.img_div)
        ImageView imgDiv;
        @BindView(R.id.tv_type_name2)
        TextView tvTypeName2;
        @BindView(R.id.tv_content2)
        TextView tvContent2;
        @BindView(R.id.tv_shuliang2)
        TextView tvShuliang2;
        @BindView(R.id.tv_time2)
        TextView tvTime2;
        @BindView(R.id.ll_holder2)
        PercentLinearLayout llHolder2;
        @BindView(R.id.img_div2)
        ImageView imgDiv2;
        @BindView(R.id.ll_touxian)
        PercentLinearLayout llTouxian;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
